{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers.convolutional import Conv2D\n",
    "from keras.layers.pooling import MaxPooling2D, AveragePooling2D\n",
    "from keras.layers.normalization import BatchNormalization\n",
    "from keras import backend as K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=8):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'input': {'shape': [8, 8, 2], 'data': [0.71533978, 0.88525366, -0.1665682, -0.78269246, -0.35938693, 0.91236145, 0.79887382, -0.32432911, -0.21462787, 0.67503409, -0.5048894, 0.94012776, 0.74460217, -0.65420208, -0.96575802, 0.04857124, -0.5143757, -0.859664, -0.70407111, 0.90069502, -0.1745674, 0.96862199, 0.13854484, -0.4568545, -0.4245596, 0.50240832, 0.32215057, -0.53878834, -0.08100389, -0.95676798, 0.50984271, 0.35938211, -0.80574471, -0.27280043, -0.79372702, -0.56424754, 0.71578398, -0.07600055, -0.28515341, -0.34649692, -0.78198919, 0.60369626, 0.51540746, -0.08121161, 0.29004911, 0.53866205, -0.26276086, -0.35291885, -0.20156365, -0.19919736, 0.43876379, -0.78627183, 0.17243849, -0.87263409, -0.78892142, 0.80502663, 0.48480321, 0.94244119, -0.7592124, 0.97657468, 0.80274984, -0.22122307, 0.37493096, 0.59471548, 0.55603145, -0.17335433, -0.74925815, -0.80077602, 0.25531068, -0.15845056, 0.03409256, -0.23668524, 0.615225, 0.60886144, 0.28578781, 0.62379334, 0.64138227, 0.04467717, 0.50887919, -0.14342325, 0.38138514, -0.36238775, 0.03310662, 0.40717212, 0.52973125, 0.07379, 0.12743602, 0.17507368, 0.47717717, 0.93970818, 0.7143391, 0.39621349, -0.83736675, -0.20192069, -0.60974976, -0.1061211, -0.63566041, -0.75096253, 0.7542665, 0.62323479, -0.86282146, 0.37112856, -0.23752737, 0.04117665, -0.34880528, 0.79890059, 0.50239488, -0.40679063, 0.77630974, -0.64125567, -0.88523725, -0.20931813, 0.62187509, 0.27437841, -0.5264844, -0.01933821, -0.54729633, -0.81122619, 0.14821742, -0.61136926, 0.24064872, 0.03565847, 0.34533281, 0.13377517, -0.79996168, -0.87663813, -0.44908027, 0.41712902]}, 'weights': [{'shape': [3, 3, 2, 4], 'data': [0.71533978, 0.88525366, -0.1665682, -0.78269246, -0.35938693, 0.91236145, 0.79887382, -0.32432911, -0.21462787, 0.67503409, -0.5048894, 0.94012776, 0.74460217, -0.65420208, -0.96575802, 0.04857124, -0.5143757, -0.859664, -0.70407111, 0.90069502, -0.1745674, 0.96862199, 0.13854484, -0.4568545, -0.4245596, 0.50240832, 0.32215057, -0.53878834, -0.08100389, -0.95676798, 0.50984271, 0.35938211, -0.80574471, -0.27280043, -0.79372702, -0.56424754, 0.71578398, -0.07600055, -0.28515341, -0.34649692, -0.78198919, 0.60369626, 0.51540746, -0.08121161, 0.29004911, 0.53866205, -0.26276086, -0.35291885, -0.20156365, -0.19919736, 0.43876379, -0.78627183, 0.17243849, -0.87263409, -0.78892142, 0.80502663, 0.48480321, 0.94244119, -0.7592124, 0.97657468, 0.80274984, -0.22122307, 0.37493096, 0.59471548, 0.55603145, -0.17335433, -0.74925815, -0.80077602, 0.25531068, -0.15845056, 0.03409256, -0.23668524]}, {'shape': [4], 'data': [-0.29605838, 0.82187325, -0.73929111, 0.44514925]}], 'expected': {'shape': [5, 5, 4], 'data': [1.18902016, 4.62244987, 0.13554579, 3.15344691, 1.18902016, 2.75681424, 2.0924654, 3.15344691, 2.26213264, 2.75681424, 2.0924654, 3.57354879, 2.26213264, 1.89210677, 0.0, 3.57354879, 0.04406729, 4.53793669, 1.82975888, 2.88932681, 0.0, 2.15149736, 0.08843648, 1.41444016, 1.69222617, 4.5220623, 2.0924654, 1.41444016, 2.4790504, 4.5220623, 2.0924654, 3.57354879, 2.4790504, 0.55329347, 0.32200605, 3.57354879, 0.59789729, 0.90968543, 0.32200605, 1.66985512, 0.34255257, 2.9345634, 0.70404929, 1.41444016, 1.69222617, 4.5220623, 0.0, 1.41444016, 2.4790504, 4.5220623, 0.0, 0.77654594, 2.4790504, 2.58233237, 0.32200605, 1.66985512, 0.59789729, 0.55329347, 0.99646097, 1.66985512, 0.34255257, 3.10465813, 0.70404929, 1.21055555, 0.51233566, 2.9345634, 0.0, 0.86158723, 1.05880022, 2.58233237, 0.0, 0.69131136, 1.05880022, 2.58233237, 0.0, 1.39277172, 0.40922442, 2.58466744, 0.99646097, 1.94778585, 0.20890906, 3.10465813, 0.66259533, 0.86158723, 0.5058974, 2.36699462, 0.0, 0.86158723, 0.56857514, 2.36699462, 0.0, 0.75011283, 0.56857514, 2.42080021, 0.76593131, 1.39589727, 0.40922442, 2.58466744, 0.76593131, 1.94778585]}}\n"
     ]
    }
   ],
   "source": [
    "data_in_shape = (8, 8, 2)\n",
    "\n",
    "conv_0 = Conv2D(4, 3, 3, activation='relu', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "pool_0 = MaxPooling2D(pool_size=(2, 2), strides=(1, 1), border_mode='valid', dim_ordering='tf')\n",
    "\n",
    "input_layer = Input(shape=data_in_shape)\n",
    "x = conv_0(input_layer)\n",
    "output_layer = pool_0(x)\n",
    "model = Model(input=input_layer, output=output_layer)\n",
    "\n",
    "np.random.seed(8000)\n",
    "data_in = 2 * np.random.random(data_in_shape) - 1\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(8000 + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(np.array([data_in]))\n",
    "\n",
    "print({\n",
    "    'input': {'data': format_decimal(data_in.ravel().tolist()), 'shape': list(data_in_shape)},\n",
    "    'weights': [{'data': format_decimal(weights[i].ravel().tolist()), 'shape': list(weights[i].shape)} for i in range(len(weights))],\n",
    "    'expected': {'data': format_decimal(result[0].ravel().tolist()), 'shape': list(result[0].shape)}\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
